package cn.sciento.farm.devicemanager.gateway.lorawan.grpc.impl;

import cn.sciento.farm.devicemanager.gateway.lorawan.config.JwtManager;
import cn.sciento.farm.devicemanager.gateway.lorawan.grpc.GrpcConstant;
import cn.sciento.farm.devicemanager.gateway.lorawan.grpc.LoraWanDeviceService;
import cn.sciento.farm.devicemanager.lorawan.*;
import io.grpc.Channel;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;

/**
 * @author wumu
 * @version V1.0
 * @date 9/17/18
 * @time 9:46 AM
 */
@Service()
public class LoraWanDeviceServiceImpl implements LoraWanDeviceService {

    @GrpcClient(GrpcConstant.GRPC_NAME)
    private Channel serverChannel;


    @Autowired
    private JwtManager jwtManager;

    volatile DeviceServiceGrpc.DeviceServiceBlockingStub stub;


    DeviceServiceGrpc.DeviceServiceBlockingStub getStub() {
        if (stub == null) {
            synchronized (this) {
                if (stub == null) {
                    stub = DeviceServiceGrpc.newBlockingStub(serverChannel);

                }
            }
        }
        stub = stub.withCallCredentials(jwtManager.getToken());
        return stub;
    }

    @Override
    public void create(CreateDeviceRequest request) {
        getStub().create(request);
    }

    @Override
    public GetDeviceResponse get(GetDeviceRequest request) {
        GetDeviceResponse response = null;
        try {
            response= getStub().get(request);
        }catch (Exception e){
            e.printStackTrace();
        }
        return response;
    }

    @Override
    public ListDeviceResponse list(ListDeviceRequest request) {
        ListDeviceResponse response = getStub().list(request);
        return response;
    }

    @Override
    public int delete(DeleteDeviceRequest request) {
        try {
            getStub().delete(request);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    public void update(UpdateDeviceRequest request) {
        getStub().update(request);
    }

    @Override
    public void createKeys(CreateDeviceKeysRequest request) {
        getStub().createKeys(request);
    }

    @Override
    public GetDeviceKeysResponse getKeys(GetDeviceKeysRequest request) {
        return getStub().getKeys(request);
    }

    @Override
    public void updateKeys(UpdateDeviceKeysRequest request) {
        getStub().updateKeys(request);
    }

    @Override
    public void deleteKeys(DeleteDeviceKeysRequest request) {
        getStub().deleteKeys(request);
    }

    @Override
    public void Activate(ActivateDeviceRequest request) {
        getStub().activate(request);
    }

    @Override
    public GetDeviceActivationResponse getActivation(GetDeviceActivationRequest request) {
        return getStub().getActivation(request);
    }

    @Override
    public GetRandomDevAddrResponse getRandomDevAddr(GetRandomDevAddrRequest request) {
        return getStub().getRandomDevAddr(request);
    }

    @Override
    public Iterator<StreamDeviceFrameLogsResponse> streamFrameLogs(StreamDeviceFrameLogsRequest request) {
        return getStub().streamFrameLogs(request);
    }

    @Override
    public void streamEventLogs(StreamDeviceEventLogsRequest request) {
        getStub().streamEventLogs(request);
    }


}
